package jdklearning.concurrent.thread;



/**
 * @author shenenlu 2021年02月09日 下午15:38:03
 */
public class JoinLearning {

    /**
     * join原理: 以线程对象作为锁,先wait,然后借助线程对象在线程终止后隐式调用线程对象的notifyAll
     *        ,运行是串行化的
     */
    public void joinPrinciple(){
        Thread t = new Thread(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("t");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t.start();
        System.out.println(t.isAlive());
        synchronized (t){
            try {
//                while (t.isAlive()){
                    t.wait();
                    System.out.println(t.isAlive());
//                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("main");
    }


    public static void main(String[] args) {
        JoinLearning joinLearning = new JoinLearning();
        joinLearning.joinPrinciple();
    }
}
